<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="db.PurreqDAO"%>
<%@page import="db.Purreq"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.MsgDAO"%>
<%@page import="db.Msg"%>
<%@page import="db.UserDAO"%>
<%@page import="db.User"%>
<%@page import="db.Assign"%>
<%@page import="db.AssignDAO"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	PurreqDAO purreqDao = (PurreqDAO)ctx.getBean("PurreqDAO");
	PurmatDAO purmatDao = (PurmatDAO)ctx.getBean("PurmatDAO");
	MsgDAO msgDao = (MsgDAO)ctx.getBean("MsgDAO");
	UserDAO userDao = (UserDAO)ctx.getBean("UserDAO");
	
	String reqCode = request.getParameter("reqCode");
	String reqDateStr = request.getParameter("reqDate");
	String tobId = request.getParameter("tobId");
	String projectId = request.getParameter("projectId");
	String receiver = request.getParameter("receiver");
	String phone = request.getParameter("phone");
	int tid = (Integer)session.getAttribute("tid");
	String supplierId = request.getParameter("supplierId");
	String explain = request.getParameter("explain");
	String maUserId = request.getParameter("maUserId");
	
	int uid = (Integer)session.getAttribute("id");
	
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	Date reqDate;
    try {
    	reqDate = df.parse(reqDateStr);   
    	
    	Purreq purreq = new Purreq();
        purreq.setDate(new Date());
        purreq.setRemark(explain);
        purreq.setPhone(phone);
        purreq.setProjectId(Integer.parseInt(projectId));
        purreq.setReceiver(receiver);
        purreq.setReqDate(reqDate);
        purreq.setState(2);
        purreq.setSupplierId(Integer.parseInt(supplierId));
        purreq.setTeamId(tid);
        purreq.setTobId(Integer.parseInt(tobId));
        purreq.setReqUserId(uid);
        purreq.setBuyState(1);
        if(maUserId != null && !"".equals(maUserId.trim())){
        	purreq.setBuyUserId(Integer.parseInt(maUserId));
        }
        purreqDao.save(purreq);
        
        purreq.setReqCode("C"+purreq.getId());
        purreqDao.attachDirty(purreq);
        
        String[] materialIds = request.getParameterValues("materialId");
        String[] numbers = request.getParameterValues("reqMatNum");
        String[] prices = request.getParameterValues("reqMatPrice");
        for(int i=0;i<materialIds.length;i++){
        	Purmat purmat = new Purmat();
        	purmat.setDate(new Date());
        	purmat.setDecrease(0);
        	purmat.setMaterialId(Integer.parseInt(materialIds[i]));
        	purmat.setNumber(Integer.parseInt(numbers[i]));
        	purmat.setPrice(Float.parseFloat(prices[i]));
        	purmat.setPurreqId(purreq.getId());
        	purmat.setState(1);
        	purmatDao.save(purmat);
        }
        
        submitMsg(9,2,"提交了一份采购申请！",session);
        
        request.setAttribute("reqCode",purreq.getReqCode());
        request.setAttribute("info","success");
    } catch (Exception ex) {
    	request.setAttribute("info","error");
    }
    
    pageContext.forward("/vm/procure/procureApp.vm");
%>